package com.atguigu.collection;

/**
 * @author durant
 * @create 2021-10-21 15:34
 */

public class Convert {
    static  int[][] ars;
    static{
        ars=new int[15][15];
        ars[4][8]=1; ars[7][5]=2; ars[5][7]=2;
        ars[7][7]=1; ars[5][8]=1; ars[7][8]=2;



    }

    public static void main(String[] args) {
        System.out.println(toString(ars));
        System.out.println("*----------------");
        int[][]sparesMatrix=convertSparesMatrix(ars);
        System.out.println(toString(sparesMatrix));
        System.out.println("*----------------");
        int[][] arrs=covertArray(sparesMatrix);
        System.out.println(toString(arrs));
    }

    private static int[][] covertArray(int[][] sparesMatrix) {
        int[][] arrs=new int[sparesMatrix[0][0]][sparesMatrix[0][1]];
        for (int i = 1; i < sparesMatrix.length; i++) {
                arrs[sparesMatrix[i][0]][sparesMatrix[i][1]]=sparesMatrix[i][2];
        }
        return arrs;
    }

    private static int[][] convertSparesMatrix(int[][] ars) {
        int Nozerosum=0;
        for (int[] row:ars){
            for (int num:row){
                if (num!=0){
                    Nozerosum++;
                }
            }
        }

        int noZeroRow=0;
        int[][] sparesMatrix=new int[Nozerosum+1][3];
        sparesMatrix[0][0]=15;
        sparesMatrix[0][1]=15;
        sparesMatrix[0][2]=Nozerosum;
        for (int i = 0; i <ars.length ; i++) {
            for (int j = 0; j <ars[i].length ; j++) {
                if(ars[i][j]!=0){
                    noZeroRow++;
                    sparesMatrix[noZeroRow][0]=i;
                    sparesMatrix[noZeroRow][1]=j;
                    sparesMatrix[noZeroRow][2]=ars[i][j];

                }
            }
        }
        return sparesMatrix;
    }
    public static String toString(int[][] ars){
        StringBuilder builder=new StringBuilder();
        for (int[] row:ars){
            for (int num:row){
                builder.append(num+"\t");
            }
                builder.append("\r\n");
        }
            return  builder.toString();
    }
}
